//
//  UserDetailViewController.m
//  Final
//
//  Created by student on 2021/12/17.
//  Copyright © 2021 Lenrony. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "UserDetailViewController.h"

@interface UserDetailViewController (){
    
    float screen_width;
    float screen_height;
}

@property(strong, nonatomic) User* user;
@property(nonatomic) BOOL canModify;

@property(strong, nonatomic) UIScrollView* scv;
@property(strong, nonatomic) UIImageView* headImgView;
@property(strong, nonatomic) UILabel* uidTitleLabel;
@property(strong, nonatomic) UILabel* nameTitleLabel;
@property(strong, nonatomic) UILabel* emailTitleLabel;
@property(strong, nonatomic) UILabel* phoneTitleLabel;
@property(strong, nonatomic) UILabel* hobbyTitleLabel;
@property(strong, nonatomic) UILabel* contentTitleLabel;
@property(strong, nonatomic) UILabel* uidLabel;
@property(strong, nonatomic) UILabel* nameLabel;
@property(strong, nonatomic) UILabel* emailLabel;
@property(strong, nonatomic) UILabel* phoneLabel;
@property(strong, nonatomic) UILabel* hobbyLabel;
@property(strong, nonatomic) UILabel* contentLabel;
@property(strong, nonatomic) UIButton* modifyBtn;

@end

@implementation UserDetailViewController

-(UserDetailViewController*) initWithUser: (User*)user
                            canBeModified: (BOOL)canModify
{
    self = [super init];
    self.user = user;
    self.canModify = canModify;
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    screen_width = [UIScreen mainScreen].bounds.size.width;
    screen_height = [UIScreen mainScreen].bounds.size.height;
    
    
    if(self.headImgView != nil){
        if(self.canModify){
            self.user = [ModelController getInstance].cur_user;
        }
        [self.headImgView removeFromSuperview];
        [self.uidLabel removeFromSuperview];
        [self.nameLabel removeFromSuperview];
        [self.emailLabel removeFromSuperview];
        [self.phoneLabel removeFromSuperview];
        [self.hobbyLabel removeFromSuperview];
        [self.contentLabel removeFromSuperview];
        [self.uidTitleLabel removeFromSuperview];
        [self.nameTitleLabel removeFromSuperview];
        [self.emailTitleLabel removeFromSuperview];
        [self.phoneTitleLabel removeFromSuperview];
        [self.hobbyTitleLabel removeFromSuperview];
        [self.contentTitleLabel removeFromSuperview];
        [self.scv removeFromSuperview];
    }
    
    self.scv = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, screen_width, screen_height)];
    self.headImgView = [[UIImageView alloc] initWithFrame:CGRectMake(screen_width / 2 - 60, 60, 120, 120)];
    self.headImgView.layer.cornerRadius = 10;
    self.headImgView.layer.masksToBounds = YES;
    self.headImgView.image = self.user.head_img;
    [self.scv addSubview:self.headImgView];
    
    self.uidLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 220, screen_width - 100, 20)];
    self.uidLabel.text = self.user.u_id;
    self.uidLabel.textAlignment = NSTextAlignmentLeft;
    [self.uidLabel setFont:[UIFont systemFontOfSize:18]];
    [self.scv addSubview:self.uidLabel];
    
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 250, screen_width - 100, 20)];
    self.nameLabel.text = self.user.name;
    if([self.user.name isEqualToString:@""]){
        self.nameLabel.text = @"暂无";
    }
    self.nameLabel.textAlignment = NSTextAlignmentLeft;
    [_nameLabel setFont:[UIFont systemFontOfSize:18]];
    [self.scv addSubview:self.nameLabel];
    
    self.emailLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 280, screen_width - 100, 20)];
    self.emailLabel.text = self.user.email;
    if([self.user.email isEqualToString:@""]){
        self.emailLabel.text = @"暂无";
    }
    self.emailLabel.textAlignment = NSTextAlignmentLeft;
    [self.emailLabel setFont:[UIFont systemFontOfSize:18]];
    [self.scv addSubview:self.emailLabel];
    
    self.phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 310, screen_width - 100, 20)];
    self.phoneLabel.text = self.user.phone;
    if([self.user.phone isEqualToString:@""]){
        self.phoneLabel.text = @"暂无";
    }
    self.phoneLabel.textAlignment = NSTextAlignmentLeft;
    [self.phoneLabel setFont:[UIFont systemFontOfSize:18]];
    [self.scv addSubview:self.phoneLabel];
    
    self.hobbyLabel = [[UILabel alloc]init];
    self.hobbyLabel.text = self.user.hobby;
    if([self.user.hobby isEqualToString:@""]){
        self.hobbyLabel.text = @"暂无";
    }
    self.hobbyLabel.numberOfLines = 0;
    CGSize size = [self.hobbyLabel.text boundingRectWithSize:CGSizeMake(screen_width - 10, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} context:nil].size;
    self.hobbyLabel.frame = CGRectMake(90, 340, screen_width - 100, size.height);
    self.hobbyLabel.textAlignment = NSTextAlignmentLeft;
    [self.hobbyLabel setFont:[UIFont systemFontOfSize:18]];
    [self.scv addSubview:self.hobbyLabel];
    
    float y = self.hobbyLabel.bounds.size.height + self.hobbyLabel.frame.origin.y + 10;
    self.contentLabel = [[UILabel alloc]init];
    self.contentLabel.text = self.user.content;
    if([self.user.content isEqualToString:@""]){
        self.contentLabel.text = @"暂无";
    }
    self.contentLabel.numberOfLines = 0;
    size = [_contentLabel.text boundingRectWithSize:CGSizeMake(screen_width - 100, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} context:nil].size;
    _contentLabel.frame = CGRectMake(90, y, screen_width - 10, size.height);
    _contentLabel.textAlignment = NSTextAlignmentLeft;
    [_contentLabel setFont:[UIFont systemFontOfSize:18]];
    [self.scv addSubview:self.contentLabel];
    
    self.uidTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 220, 80, 20)];
    self.uidTitleLabel.text = @"学号：";
    self.uidTitleLabel.textAlignment = NSTextAlignmentLeft;
    [self.uidTitleLabel setFont:[UIFont systemFontOfSize:18]];
    [self.scv addSubview:self.uidTitleLabel];
    
    self.nameTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 250, 80, 20)];
    self.nameTitleLabel.text = @"用户名：";
    self.nameTitleLabel.textAlignment = NSTextAlignmentLeft;
    [self.nameTitleLabel setFont:[UIFont systemFontOfSize:18]];
    [self.scv addSubview:self.nameTitleLabel];
    
    self.emailTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 280, 80, 20)];
    self.emailTitleLabel.text = @"邮箱：";
    self.emailTitleLabel.textAlignment = NSTextAlignmentLeft;
    [self.emailTitleLabel setFont:[UIFont systemFontOfSize:18]];
    [self.scv addSubview:self.emailTitleLabel];
    
    self.phoneTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 310, 80, 20)];
    self.phoneTitleLabel.text = @"电话：";
    self.phoneTitleLabel.textAlignment = NSTextAlignmentLeft;
    [self.phoneTitleLabel setFont:[UIFont systemFontOfSize:18]];
    [self.scv addSubview:self.phoneTitleLabel];
    
    self.hobbyTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 340, 80, 20)];
    self.hobbyTitleLabel.text = @"爱好：";
    self.hobbyTitleLabel.textAlignment = NSTextAlignmentLeft;
    [self.hobbyTitleLabel setFont:[UIFont systemFontOfSize:18]];
    [self.scv addSubview:self.hobbyTitleLabel];
    
    y = _hobbyLabel.bounds.size.height + _hobbyLabel.frame.origin.y + 10;
    self.contentTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, y, 80, 20)];
    self.contentTitleLabel.text = @"简介：";
    self.contentTitleLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentTitleLabel setFont:[UIFont systemFontOfSize:18]];
    [self.scv addSubview:self.contentTitleLabel];
    
    
    if(self.canModify){
        [self.scv addSubview:self.modifyBtn];
        float y = self.modifyBtn.frame.origin.y + self.modifyBtn.bounds.size.height + 100;
        self.scv.contentSize = CGSizeMake(screen_width, y);
    }
    else{
        float y = self.contentLabel.frame.origin.y + self.contentLabel.bounds.size.height + 100;
        self.scv.contentSize = CGSizeMake(screen_width, y);
    }
    [self.view addSubview:self.scv];
}

-(void)modifyClicked
{
    ModifyUserViewController* mfvc = [[ModifyUserViewController alloc]initWithUser:self.user];
    [self.navigationController pushViewController:mfvc animated:YES];
}

//LazyLoad

-(UIButton*) modifyBtn
{
    if(_modifyBtn == nil){
        float y = self.contentLabel.bounds.size.height + self.contentLabel.frame.origin.y + 40;
        _modifyBtn = [[UIButton alloc]initWithFrame:CGRectMake(screen_width / 2 - 60, y, 120, 30)];
        [_modifyBtn setTitle:@"修改资料" forState:UIControlStateNormal];
        _modifyBtn.backgroundColor = [UIColor colorWithRed:0 green:0.7f blue:0 alpha:1];
        _modifyBtn.layer.cornerRadius = 5;
        [_modifyBtn addTarget:self action:@selector(modifyClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _modifyBtn;
}

@end
